package T4.EjemplosDeIO;


import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Ejemplo4 {

    public static void main(String[] args) {

        File file1 = new File("C:/original.txt");
        File file2 = new File("C:/copia.txt");
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        PrintWriter printWriter = null;
        try {
            fileInputStream = new FileInputStream(file1);
            inputStreamReader = new InputStreamReader(fileInputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            String texto = bufferedReader.readLine();
            printWriter = new PrintWriter(file2);

            do {
                System.out.println(texto);
                printWriter.println(texto);
            } while ((texto = bufferedReader.readLine()) != null);
        } catch (IOException ex) {
            System.out.println("Error durante el proceso de copia");
            ex.printStackTrace();
        } finally {
            try {
                if (inputStreamReader != null){
                    inputStreamReader.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(Ejemplo4.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
                if (printWriter != null) {
                printWriter.close();
            }
        }
    }
}